package com.superyee.commonlib.logic;

import android.text.TextUtils;

import androidx.annotation.IntRange;

import com.alibaba.fastjson.JSON;
import com.superyee.commonlib.constants.AppConfig;
import com.superyee.commonlib.constants.StaticConstant;
import com.superyee.commonlib.http.HttpParams;
import com.superyee.commonlib.http.HttpResponseListener;
import com.superyee.commonlib.observer.ObserverManager;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

public abstract class BaseLogic<T> extends HttpResponseListener<T> {
    public static int SUCCESS_CODE = 200;


    public static HttpParams getCommonParams() {
        HttpParams params = new HttpParams();
//        String token = SpUtils.getString(StaticConstant.SP.ACCESS_TOKEN, "");
//        params.addParam(StaticConstant.SP.ACCESS_TOKEN, token);
//        params.addParam(StaticConstant.SP.TOKEN, token);
        params.addParam("platform", "android");
        return params;
    }

    public JSONObject getCommonJsonParams() {
        JSONObject json = new JSONObject();
        try {
//            String token = SpUtils.getString(StaticConstant.SP.ACCESS_TOKEN, "");
//            json.put(StaticConstant.SP.ACCESS_TOKEN, token);
//            json.put(StaticConstant.SP.NET_EASE_TOKEN, token);
            json.put("platform", "android");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return json;
    }


    public static final int MODE_SHIFT = 16;
    private static final int MODE_MASK = 0xFFFF << MODE_SHIFT;

    public static int makeAction(@IntRange(from = 0, to = (1 << MODE_SHIFT) - 1) int uiAction, int logicAction) {
        logicAction = logicAction << MODE_SHIFT;
        return (uiAction & ~MODE_MASK) | (logicAction & MODE_MASK);
    }

    public static int getUiAction(int action) {
        return (action & ~MODE_MASK);
    }

    public static int getLogicAction(int action) {
        int result = (action & MODE_MASK) >> MODE_SHIFT;
        if (result == 0) {
            result = action;
        }
        return result;
    }

    public static String getMsg(String response) {
        return getError(response);
    }

    public static String getError(String response) {
        com.alibaba.fastjson.JSONObject jsonObject = null;
        try {
            jsonObject = JSON.parseObject(response);
        } catch (Exception e) {
            CrashReport.postCatchedException(new Throwable(e.getMessage() + "   result: ==== " + response));
        }
        if (jsonObject == null) {
            return response;
        }
        String error = jsonObject.getString(StaticConstant.Response.ERRORS);
        if (TextUtils.isEmpty(error)) {
            error = jsonObject.getString(StaticConstant.Response.MESSAGE);
        }
        if (TextUtils.isEmpty(error)) {
            error = jsonObject.getString(StaticConstant.Response.ERROR);
        }
        String debug = jsonObject.getString(StaticConstant.Response.DEBUG);
        if (!TextUtils.isEmpty(debug)) {
            error = error + "(" + debug + ")";
        }
        if (TextUtils.isEmpty(error)) {
//                        error = AppConfig.UNKNOWNEXCEPTION;
            error = response;
        }
        return error;
    }

    @Override
    public void onStart(int action, String url) {
        ObserverManager.getInstence().notifyUi(action, null, ObserverManager.START);
    }

    @Override
    public void onFinish(int action) {
        ObserverManager.getInstence().notifyUi(action, null, ObserverManager.FINISH);
    }

    @Override
    public void onFailure(int resultCode, int action, String error, Throwable throwable) {
        ObserverManager.getInstence().notifyUi(action, error, ObserverManager.FAILURE);
    }

    @Override
    public void noNetwork(int action) {
        ObserverManager.getInstence().notifyUi(action, AppConfig.ERROR_NO_NETWORK, ObserverManager.NONETWORK);
    }

    @Override
    public void onSuccess(int statusCode, int action, T result, Object obj) {
        success(statusCode, action, result, obj);
    }

    protected abstract void success(int statusCode, int action, T result, Object obj);

}
